from typing import Callable

from fastapi import FastAPI

from app.core.redis_conn import redis_helper
from app.core.task.scheduler_manager import start_scheduler, stop_scheduler


def create_start_app_handler(app: FastAPI) -> Callable:
    async def start_app() -> None:
        # 确保Redis连接已初始化
        await start_scheduler()
        app.state.redis_helper = redis_helper

    return start_app


def create_stop_app_handler(app: FastAPI) -> Callable:
    async def stop_app() -> None:
        # 关闭Redis连接
        await redis_helper.close()
        await stop_scheduler()

    return stop_app
